home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / GAME / Xconq 7.0.1.sit / Xconq 7.0.1 / doc / kwic.c < prev    next >
C/C++ Source or Header  |  1995-08-22  |  4KB  |  154 lines

  1. /* Simple KWIC generator, uses stdin/stdout only. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. /* Make some C compilers happier. */
  8. char *strchr();
  9.  
  10. /* Every symbol must be no more than this long. */
  11.  
  12. #define SYMLEN 40
  13.  
  14. #define EXTRALEN 20
  15.  
  16. #define MAXSYMBOLS 1000
  17.  
  18. char *arr;
  19.  
  20. char *prefix = NULL;
  21.  
  22. char *suffix = NULL;
  23.  
  24. int pad = 0;
  25.  
  26. int dots = 0;
  27.  
  28. int longest = 0;
  29.  
  30. int breakup = 0;
  31.  
  32. compare_names(n1, n2)
  33. char *n1, *n2;
  34. {
  35.     return strcmp(n1 + SYMLEN, n2 + SYMLEN);
  36. }
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     int i, j, linesize, bufsize, maxi, blanks;
  43.     char name[SYMLEN + 1 + EXTRALEN + 1], *extra, *str;
  44.  
  45.     /* Parse a couple args if supplied. */
  46.     for (i = 1; i < argc; ++i) {
  47.     if (strcmp(argv[i], "-prefix") == 0) {
  48.         prefix = argv[++i];
  49.     } else if (strcmp(argv[i], "-suffix") == 0) {
  50.         suffix = argv[++i];
  51.     } else if (strcmp(argv[i], "-breakup") == 0) {
  52.         breakup = 1;
  53.     } else if (strcmp(argv[i], "-dots") == 0) {
  54.         dots = 1;
  55.     } else if (strcmp(argv[i], "-pad") == 0) {
  56.         pad = 1;
  57.     }
  58.     }
  59.     linesize = 2 * SYMLEN + 1 + EXTRALEN + 1;
  60.     bufsize = MAXSYMBOLS * 5 * linesize;
  61.     arr = (char *) malloc(bufsize * sizeof(char));
  62.     for (i = 0; i < bufsize; ++i)
  63.       arr[i] = ' ';
  64.     i = 0;
  65.     while (fgets(name, SYMLEN+1+EXTRALEN+1, stdin) != NULL) {
  66.     if (name[strlen(name)-1] == '¥n')
  67.       name[strlen(name)-1] = '¥0';
  68.     extra = strchr(name, ' ');
  69.     if (extra != NULL) {
  70.         /* Replace the blank with an EOS, point after it. */
  71.         *extra = '¥0';
  72.         ++extra;
  73.         if (strlen(extra) > EXTRALEN) {
  74.         extra[EXTRALEN-1] = '¥0';
  75.         }
  76.     }
  77.     if (strlen(name) > SYMLEN) {
  78.         fprintf(stderr, "Symbol too long¥n");
  79.         continue;
  80.     }
  81.     /* See if this is the longest name so far. */
  82.     if (strlen(name) > longest)
  83.       longest = strlen(name);
  84.     /* Put out the name undisplaced. */
  85.     memcpy(arr + i * linesize + SYMLEN, name, strlen(name));
  86.     *(arr + i * linesize + 2 * SYMLEN) = '¥0';
  87.     *(arr + i * linesize + 2 * SYMLEN + 1) = '¥0';
  88.     if (extra != NULL)
  89.       strcpy(arr + i * linesize + 2 * SYMLEN + 1, extra);
  90.     ++i;
  91.     /* Scan down the name looking for hyphens. */
  92.     for (j = 0; name[j] != '¥0'; ++j) {
  93.         if (name[j] == '-') {
  94.         /* Put out the name shifted left by position of hyphen. */
  95.         memcpy(arr + i * linesize + SYMLEN - j-1, name, strlen(name));
  96.         *(arr + i * linesize + 2 * SYMLEN) = '¥0';
  97.         *(arr + i * linesize + 2 * SYMLEN + 1) = '¥0';
  98.         if (extra != NULL)
  99.           strcpy(arr + i * linesize + 2 * SYMLEN + 1, extra);
  100.         ++i;
  101.         }
  102.     }
  103.     }
  104.     /* Sort everything. */
  105.     maxi = i;
  106.     qsort(arr, maxi, linesize, compare_names);
  107.     for (j = 0; j < MAXSYMBOLS; ++j) {
  108.     if (arr[j] == '¥n')
  109.       arr[j] = '&';
  110.     }
  111.     blanks = 99;
  112.     for (i = 0; i < maxi; ++i) {
  113.     str = arr + i * linesize + SYMLEN - longest;
  114.     for (j = 0; j < longest; ++j)
  115.       if (str[j] != ' ')
  116.         break;
  117.     if (j < blanks)
  118.       blanks = j;
  119.     }
  120.     /* Dump out all the lines. */
  121.     if (breakup)
  122.       fputs("@smallexample¥n", stdout);
  123.     for (i = 0; i < maxi; ++i) {
  124.     if (prefix != NULL)
  125.       fputs(prefix, stdout);
  126.     str = arr + i * linesize + SYMLEN - longest;
  127.     *(str + 2 * longest) = '¥0';
  128.     if (dots) {
  129.         for (j = strlen(str) - 1; j >= 0; --j) {
  130.         if (str[j] != ' ')
  131.           break;
  132.         if (j % 2 == 0 && str[j-1] == ' ' && str[j+1] == ' ')
  133.           str[j] = '.';
  134.         }
  135.     }
  136.     fputs(str + blanks, stdout);
  137.     if (suffix != NULL)
  138.       fputs(suffix, stdout);
  139.     if (pad) {
  140.         for (j = 0; j < pad; ++j)
  141.           fputs(" ", stdout);
  142.     }
  143.     fputs(arr + i * linesize + 2 * SYMLEN + 1, stdout);
  144.     fputs("¥n", stdout);
  145.     if (breakup && i > 0 && i % 50 == 0) {
  146.         fputs("@end smallexample¥n", stdout);
  147.         fputs("@smallexample¥n", stdout);
  148.     }
  149.     }
  150.     if (breakup)
  151.       fputs("@end smallexample¥n", stdout);
  152.     exit(0);
  153. }
  154.